

# Random Effect Canton
raw.re.canton <- ranef(model.r)$canton
namesCan <- rownames(raw.re.canton)
raw.re.canton <- cbind(as.numeric(namesCan), raw.re.canton)
re.canton <- rep(0,26)
	for (t in raw.re.canton[,1]){
		re.canton[t] <- raw.re.canton[raw.re.canton[,1]==t,2]
	}

# Random Effect Education
raw.re.educ <- ranef(model.r)$x1.education
namesEduc <- rownames(ranef(model.r)$x1.education)
raw.re.educ <- cbind(as.numeric(namesEduc),raw.re.educ)
re.educ <- rep(0,6)
	for (t in raw.re.educ[,1]){
		re.educ[t] <- raw.re.educ[raw.re.educ[,1]==t,2]
	}
	
# Random Effect Region
raw.re.reg <- ranef(model.r)$region
namesReg <- rownames(raw.re.reg)
raw.re.reg <- cbind(as.numeric(namesReg), raw.re.reg)
re.reg <- rep(0,7)
	for (t in raw.re.reg[,1]){
		re.reg[t] <- raw.re.reg[raw.re.reg[,1]==t,2]
	}

# Random Effect Age
raw.re.age <- ranef(model.r)$x1.age
namesAge <- rownames(raw.re.age)
raw.re.age <- cbind(as.numeric(namesAge), raw.re.age)
re.age <- rep(0,4)
	for (t in raw.re.age[,1]){
		re.age[t] <- raw.re.age[raw.re.age[,1]==t,2]
	}
	
# Random Effect Gender
re.female1 <- ranef(model.r)$x1.gender
re.female <- rep(NA,2)
re.female[1] <- re.female1[[1]][1]
re.female[2] <- re.female1[[1]][2]

# Random Effect Party- ID
raw.re.party <-  ranef(model.r)$x1.partyL
namesParty <- rownames(raw.re.party)
raw.re.party <- cbind(as.numeric(namesParty), raw.re.party)
re.party <- rep(0,7)
	for (t in raw.re.party[,1]){
		re.party[t] <- raw.re.party[raw.re.party[,1]==t,2]
	}

# Random Effect INCOME - $$$$
raw.re.money <-  ranef(model.r)$x1.money
namesMoney <- rownames(raw.re.money)
raw.re.money <- cbind(as.numeric(namesMoney), raw.re.money)
re.money <- rep(0,7)
	for (t in raw.re.money[,1]){
		re.money[t] <- raw.re.money[raw.re.money[,1]==t,2]
	}
re.money <- re.money[1:5]




# Read-in context data in small data form (only 26 rows)
data3 <- read.dta("Contextdata1.dta")
jjj <- data.run$JAHR[1]
data3 <- data3[data3$JAHR==jjj,]
X21 <- data3$leftvote_share
X22 <- data3$romkath_share
X23 <- data3$german_share
X24 <- data3$leftvote_share
region.pointer <- data3$region
regional.re <- rep(NA,26)
for (r in 1:26){
	regional.re[r] <- re.reg[region.pointer[r]]
}



XX <- cbind(X21,X22,X23,X24) #,X25,X26,X27)
xLx <- cbind(1,XX[,c(3,4)])
pred.cantonal <- xLx%*%fixef(model.r) + re.canton + regional.re


# type-specific prediction --- synthetic joint has 288 ideal types; MONEY(5), EDUC(6), AGE(4), FEMALE(2), PARTY(6)
money.mark <- c(rep(1,288),rep(2,288),rep(3,288),rep(4,288),rep(5,288))
educ.mark <- rep(c(rep(1,48),rep(2,48),rep(3,48),rep(4,48),rep(5,48),rep(6,48)),5)
age.mark <- rep(c(rep(c(rep(1,12),rep(2,12),rep(3,12),rep(4,12)),6)),5)
female.mark <- rep(c(rep(c(rep(1,6),rep(2,6)),24)),5)
party.mark <- rep(c(rep(c(1:6),48)),5)
block.indiv <- rep(NA,1440)
	for (t in 1:1440){
		block.indiv[t] <- re.money[money.mark[t]] + re.educ[educ.mark[t]] + re.age[age.mark[t]] + re.female[female.mark[t]] + re.party[party.mark[t]]
	}


##############################################################################################################################

# read-in census data
# Save the first line of each canton
N <- rep(NA,26)
for (i in 1:26){
	n <- 3+i*2411
	N[i] <- n	
}
N <- c(N,65100)

# create container, voter types disaggregated * cantons
pull.out.BIG <- matrix(NA,12,26)

# read out canton by canton, creating "pull.out.BIG" (192*26), for every type and every canton a head count
for (k in 1:26){
	eins <- k
	zwei <- k+1
	block.use <- as.matrix(block[(N[eins]):(N[zwei]-1),])
	# here is all the hard work by hand
	source("census_pull_out_command_CH_marginal.R")
	pull.out.BIG[,k] <- pull.out.d
}

### Adding party proportions porportions to "pull.out.BIG"
data.party <- read.dta("parties in cantons.dta")
data.party <- data.party[data.party$JAHR==data.run$JAHR[1],]
party.shares <- data.party[,c(39:44)]

# down-weighting party
#	*!*
#	*!*
#party.shares <- party.shares*0.6
#	*!*
#	*!*
#	*!*


pull.out.BIG <- rbind(pull.out.BIG,t(party.shares))

### Adding $$$$ proportions porportions to "pull.out.BIG"
money.shares <- data3[,c(20:24)]

pull.out.BIG <- rbind(pull.out.BIG,t(money.shares))



pull.out.BIG.educ <- pull.out.BIG[1:6,]
pull.out.BIG.age  <- pull.out.BIG[7:10,]
pull.out.BIG.female  <- pull.out.BIG[11:12,]
pull.out.BIG.party  <- pull.out.BIG[13:18,]
pull.out.BIG.money  <- pull.out.BIG[19:23,]

# synthetic joint for 1440*26
pull.out.synth <- rep(NA, 1440*26)
	for (q in 1:26){
		aa <- 1+((q-1)*1440)
		bb <- (q*1440)
		synthi <- rep(NA, 1440)
		for (k in 1:1440){
			synthi[k] <- pull.out.BIG.money[money.mark[k],q] *pull.out.BIG.educ[educ.mark[k],q] * pull.out.BIG.age[age.mark[k],q] * pull.out.BIG.female[female.mark[k],q] * pull.out.BIG.party[party.mark[k],q]
			synthiP <- synthi/sum(synthi)
		}
	pull.out.synth[aa:bb] <- synthiP
	}

# combining individual and contextual level
	A <- c(NA)
	for (s in 1:26){
		tata <- rep(pred.cantonal[s], 1440)
		A <- c(A,tata)	
	}
	y.lat.cont  <- A[-1]

	y.lat.ALL <- y.lat.cont + rep(block.indiv,26)
	y.predicted <- pnorm(y.lat.ALL)

	y.predicted.weighted <- pull.out.synth * y.predicted

	prediction.X <- rep(NA,26)
	for (q in 1:26){
		aa <- 1+((q-1)*1440)
		bb <- (q*1440)
		prediction.X[q] <- sum(y.predicted.weighted[aa:bb])
	}

predictionMrsP <- prediction.X
